package smartcounters.androidclient;

import smartcounters.androidclient.WebServices.ConnectionError;
import smartcounters.androidclient.WebServices.InformationUpdaterWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.Configuration;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;

public class CreateAccountActivity extends Activity implements OnClickListener, OnResultReceiver{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_account_layout);
        
        Button createAccountButton = (Button)findViewById(R.id.buttonCreateAccount);
        createAccountButton.setOnClickListener(this);
    }

	public void onClick(View v) {
		EditText countryEditText = (EditText)findViewById(R.id.countryEditText);
		EditText cityEditText = (EditText)findViewById(R.id.cityEditText);
		EditText streetEditText = (EditText)findViewById(R.id.streetEditText);
		EditText houseEditText = (EditText)findViewById(R.id.houseEditText);
		EditText apartmentEditText = (EditText)findViewById(R.id.apartmentEditText);
		EditText accountPhoneEditText = (EditText)findViewById(R.id.accountPhoneEditText);
		EditText usersAssociatedWithEditText = (EditText)findViewById(R.id.usersAssociatedWithEditText);
		Button createAccountButton = (Button)findViewById(R.id.buttonCreateAccount);
		
		int[] users = null;
		
		String[] usersStrings = usersAssociatedWithEditText.getText().toString().split(",");
		if (usersStrings.length == 1 && usersStrings[0].length() == 0)
		{
			users = new int[0];
		}
		else
		{
			users = new int[usersStrings.length];
			for (int i = 0; i < usersStrings.length; i++){
				users[i] = Integer.parseInt(usersStrings[i]);
			}	
		}
		
		if (v.getId() == R.id.buttonCreateAccount){
			countryEditText.setEnabled(false);
			cityEditText.setEnabled(false);
			streetEditText.setEnabled(false);
			houseEditText.setEnabled(false);
			apartmentEditText.setEnabled(false);
			accountPhoneEditText.setEnabled(false);
			usersAssociatedWithEditText.setEnabled(false);
			createAccountButton.setEnabled(false);
			
			TextView messageTextView = (TextView)findViewById(R.id.accountCreateMessageTextView);
			messageTextView.setText("Please wait...");
			
			InformationUpdaterWebServiceClient updaterClient = new InformationUpdaterWebServiceClient(Configuration.getInstance().getServerAddress());
			updaterClient.RegisterResultReceiver(this);
			
			int apartment = -1;
			
			try{
				apartment = Integer.parseInt(apartmentEditText.getText().toString());
			}
			catch (Exception ex){
				apartment = -1;
			}
			
			if (apartment == -1){
				updaterClient.CreateAccountWithoutApartment(
						Configuration.getInstance().getUserCredentials(),
						countryEditText.getText().toString(),
						cityEditText.getText().toString(),
						streetEditText.getText().toString(),
						Integer.parseInt(houseEditText.getText().toString()),
						accountPhoneEditText.getText().toString(),
						users);
			}
			else{
				updaterClient.CreateAccountWithApartment(
						Configuration.getInstance().getUserCredentials(),
						countryEditText.getText().toString(),
						cityEditText.getText().toString(),
						streetEditText.getText().toString(),
						Integer.parseInt(houseEditText.getText().toString()),
						Integer.parseInt(apartmentEditText.getText().toString()),
						accountPhoneEditText.getText().toString(),
						users);
			}
		}
	}

	public void ReceiveResult(Message i_Msg) {
		EditText countryEditText = (EditText)findViewById(R.id.countryEditText);
		EditText cityEditText = (EditText)findViewById(R.id.cityEditText);
		EditText streetEditText = (EditText)findViewById(R.id.streetEditText);
		EditText houseEditText = (EditText)findViewById(R.id.houseEditText);
		EditText apartmentEditText = (EditText)findViewById(R.id.apartmentEditText);
		EditText accountPhoneEditText = (EditText)findViewById(R.id.accountPhoneEditText);
		EditText usersAssociatedWithEditText = (EditText)findViewById(R.id.usersAssociatedWithEditText);
		Button createAccountButton = (Button)findViewById(R.id.buttonCreateAccount);
		
		if (!(i_Msg.obj instanceof ConnectionError)){
			TextView messageTextView = (TextView)findViewById(R.id.accountCreateMessageTextView);
			messageTextView.setText("The requested account was created.");
			
			countryEditText.setText("");
			cityEditText.setText("");
			streetEditText.setText("");
			houseEditText.setText("");
			apartmentEditText.setText("");
			accountPhoneEditText.setText("");
			usersAssociatedWithEditText.setText("");
		}
		else{
			TextView messageTextView = (TextView)findViewById(R.id.accountCreateMessageTextView);
			messageTextView.setText("An error occured.");
		}
		
		countryEditText.setEnabled(true);
		cityEditText.setEnabled(true);
		streetEditText.setEnabled(true);
		houseEditText.setEnabled(true);
		apartmentEditText.setEnabled(true);
		accountPhoneEditText.setEnabled(true);
		usersAssociatedWithEditText.setEnabled(true);
		createAccountButton.setEnabled(true);
	}
}